package com.yeyaomai.dksns.flowis.servlet;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import org.codehaus.jackson.map.ObjectMapper;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.yeyaomai.dksns.flowis.domain.UserReturnRate;
import com.yeyaomai.dksns.flowis.domain.VisitDepth;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.service.UserReturnRateService;
import com.yeyaomai.dksns.flowis.service.VisitDepthService;
import com.yeyaomai.dksns.flowis.util.Globals;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * @author zuoshuai@yeyaomai.com
 */
@Controller
@RequestMapping("/loyalty/*")
public class CustomerLoyaltyController {

	//private static Logger logger = LoggerFactory.getLogger(CustomerLoyaltyController.class);
	// 用户访问深度
	@Autowired
	private UserReturnRateService userReturnRateService;
	// 用户回头率
	@Autowired
	private VisitDepthService visitDepthService;

	//private static ObjectMapper objectMapper = new ObjectMapper();

	@RequestMapping(value = "/user-return-rate")
	public ModelAndView userReturnRate(HttpServletRequest request, HttpServletResponse response,
			FlowisQueryOrder order, ModelMap modelMap) {
		ModelAndView mav = new ModelAndView("user-back");
		String st = request.getParameter("time");
		String et = request.getParameter("endtime");
		String today = Globals.FORMAT_TYPE_A.format(new Date());
		int totalUniqueHost = 0;
		int totalIp = 0;
		int totalPv = 0;

		// 获取今天时间段
		if (null == st || null == et) {

			String[] retDate = VelocityToolDateUtils.getDateRange(new Date(), 1).split(",");
			st = retDate[0];
			et = retDate[1];
		}

		List<UserReturnRate> userReturnRateList = userReturnRateService.getByDate(st, et);

		for (UserReturnRate userReturnRate : userReturnRateList) {
			totalUniqueHost = totalUniqueHost + userReturnRate.getUniqueHost();
			totalIp = totalIp + userReturnRate.getIp();
			totalPv = totalPv + userReturnRate.getPv();
		}

		// 总ip，总独立访客，总pv
		mav.addObject("totalUniqueHost", totalUniqueHost);
		mav.addObject("totalIp", totalIp);
		mav.addObject("totalPv", totalPv);
		// 获取当天的数据
		mav.addObject("date", st);
		mav.addObject("enddate", et);
		mav.addObject("today", today);
		mav.addObject("userReturnRateList", userReturnRateList);
		return mav;
	}

	@RequestMapping(value = "/user-visit-depth")
	public ModelAndView userVisitDepth(HttpServletRequest request, HttpServletResponse response,
			FlowisQueryOrder order, ModelMap modelMap) {
		ModelAndView mav = new ModelAndView("user-visit");

		String st = request.getParameter("time");
		String et = request.getParameter("endtime");
		String today = Globals.FORMAT_TYPE_A.format(new Date());

		// 获取今天时间段
		if (null == st || null == et) {

			String[] retDate = VelocityToolDateUtils.getDateRange(new Date(), 1).split(",");
			st = retDate[0];
			et = retDate[1];
		}

		List<VisitDepth> visitDepthServiceList = visitDepthService.getByDate(st, et);
		int totalUniqueHost = 0;
		for (VisitDepth visitDepth : visitDepthServiceList) {
			totalUniqueHost = totalUniqueHost + visitDepth.getUniqueHost();
		}

		mav.addObject("date", st);
		mav.addObject("enddate", et);
		mav.addObject("today", today);
		mav.addObject("visitDepthServiceList", visitDepthServiceList);
		mav.addObject("totalUniqueHost", totalUniqueHost);
		return mav;
	}
}
